﻿using QBZ.Evaluation.Models;
using QBZ.Evaluation.Repositories.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QBZ.Evaluation.Repositories
{
    public class SkillRepository
    {
        private dbEvaluationContext _db;
        public SkillRepository(dbEvaluationContext db)
        {
            _db = db;
        }

        public bool AddSkillParent(SkillParentModel skillParent)
        {
            _db.TbSkillParent.Add(new TbSkillParent
            {
                CreateBy = 0,
                CreateOn = DateTime.Now,
                Guid = Guid.NewGuid(),
                SkillParentDescription = skillParent.SkillParentDescription,
                SkillParentIsDelete = false,
                SkillParentName = skillParent.SkillParentName,
            });
            return _db.SaveChanges() == 1;
        }

        public int AddSkill(SkillModel skill)
        {
            var tbskill = new TbSkill
            {
                CreateBy = 0,
                CreateOn = DateTime.Now,
                Guid = Guid.NewGuid(),
                SkillDescription = skill.SkillDescription,
                SkillIsDelete = false,
                SkillName = skill.SkillName,
                SkillParentId = skill.SkillParentId,
            };
            _db.TbSkill.Add(tbskill);
            _db.SaveChanges();
            return tbskill.SkillId;
        }

        public List<SkillModel> GetAll()
        {
            var skillChilds =
                (from rc in _db.TbSkillChild
                 where rc.SkillChildIsDelete == false
                 orderby rc.SkillId
                 select new SkillChildModel
                 {
                     SkillId = rc.SkillId,
                     SkillChildId = rc.SkillChildId,
                     SkillChildName = rc.SkillChildName,
                     SkillChildDescription = rc.SkillChildDescription
                 }).ToList();

            var skillIds = skillChilds.Select(rc => rc.SkillId).Distinct().ToList();

            var skills =
                (from r in _db.TbSkill
                 where r.SkillIsDelete == false && skillIds.Contains(r.SkillId)
                 select new SkillModel
                 {
                     SkillId = r.SkillId,
                     SkillName = r.SkillName,
                     SkillDescription = r.SkillDescription,
                 }).ToList();

            foreach (var skill in skills)
            {
                skill.SkillChilds = (from rch in skillChilds where rch.SkillId == skill.SkillId select rch).ToList();
            }
            skills[0].SkillDescription = Guid.NewGuid().ToString();
            return skills;
        }

        public bool DeleteSkill(int skillId)
        {
            var row = _db.TbSkill.Where(_ => _.SkillId == skillId).FirstOrDefault();
            if (row != null)
            {
                row.SkillIsDelete = true;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public bool AddSkillDetails(List<SkillChildModel> childs)
        {
            foreach(var skillChild in childs)
            {
                _db.TbSkillChild.Add(new TbSkillChild
                {
                    CreateBy = 0,
                    CreateOn = DateTime.Now,
                    Guid = Guid.NewGuid(),
                    LevelId = skillChild.LevelId,
                    SkillChildDescription = skillChild.SkillChildDescription,
                    SkillChildIsDelete = false,
                    SkillChildName = skillChild.SkillChildName,
                    SkillId = skillChild.SkillId
                });
            }
            return _db.SaveChanges() >= 1;
        }

        public bool MoveSkillDetail(SkillChildModel skillChild)
        {
            var row = _db.TbSkillChild.Where(_ => _.SkillChildId == skillChild.SkillChildId).FirstOrDefault();
            if (row != null)
            {
                row.SkillId = skillChild.SkillId;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public bool MoveSkill(SkillModel skillModel)
        {
            var row = _db.TbSkill.Where(_ => _.SkillId == skillModel.SkillId).FirstOrDefault();
            if (row != null)
            {
                row.SkillParentId = skillModel.SkillParentId;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public bool DeleteSkillParent(int skillParentId)
        {
            var row = _db.TbSkillParent.Where(_ => _.SkillParentId == skillParentId).FirstOrDefault();
            if (row != null)
            {
                row.SkillParentIsDelete = true;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public bool DeleteSkillDetail(int skillDetailId)
        {
            var row = _db.TbSkillChild.Where(_ => _.SkillChildId == skillDetailId).FirstOrDefault();
            if (row != null)
            {
                row.SkillChildIsDelete = true;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public bool EditSkillDetail(SkillChildModel skillChild)
        {
            var row = _db.TbSkillChild.Where(_ => _.SkillChildId == skillChild.SkillChildId).FirstOrDefault();
            if (row != null)
            {
                row.SkillChildName = skillChild.SkillChildName;
                row.SkillChildDescription = skillChild.SkillChildDescription;
                row.LevelId = skillChild.LevelId;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public bool AddSkillDetail(SkillChildModel skillChild)
        {
            _db.TbSkillChild.Add(new TbSkillChild
            {
                CreateBy = 0,
                CreateOn = DateTime.Now,
                Guid = Guid.NewGuid(),
                LevelId = skillChild.LevelId,
                SkillChildDescription = skillChild.SkillChildDescription,
                SkillChildIsDelete = false,
                SkillChildName = skillChild.SkillChildName,
                SkillId = skillChild.SkillId
            });
            return _db.SaveChanges() == 1;
        }

        public List<SkillChildModel> GetSkillDetails(int skillId)
        {
            return (from s in _db.TbSkillChild
                    where s.SkillChildIsDelete == false && (skillId == 0 || s.SkillId == skillId)
                    select new SkillChildModel
                    {
                        SkillChildName = s.SkillChildName,
                        SkillChildDescription = s.SkillChildDescription,
                        LevelId = s.LevelId,
                        SkillChildId = s.SkillChildId,
                    }).ToList();
        }

        public bool EditSkillParent(SkillParentModel skillParent)
        {
            var row = _db.TbSkillParent.Where(_ => _.SkillParentId == skillParent.SkillParentId).FirstOrDefault();
            if (row != null)
            {
                row.SkillParentName = skillParent.SkillParentName;
                row.SkillParentDescription = skillParent.SkillParentDescription;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public bool EditSkill(SkillModel skill)
        {
            var row = _db.TbSkill.Where(_ => _.SkillId == skill.SkillId).FirstOrDefault();
            if (row != null)
            {
                row.SkillName = skill.SkillName;
                row.SkillDescription = skill.SkillDescription;
                row.SkillParentId = skill.SkillParentId;
                row.UpdateBy = 0;
                row.UpdateOn = DateTime.Now;
            };
            return _db.SaveChanges() == 1;
        }

        public List<SkillParentModel> GetSkillParents()
        {
            return (from s in _db.TbSkillParent
            where s.SkillParentIsDelete == false
            select new SkillParentModel
            {
                SkillParentDescription = s.SkillParentDescription,
                SkillParentId = s.SkillParentId,
                SkillParentName = s.SkillParentName
            }).ToList();
        }

        public List<SkillModel> GetSkills(int parentId =0)
        {
            return (from s in _db.TbSkill
                    where s.SkillIsDelete == false &&(parentId==0 || s.SkillParentId == parentId)
                    select new SkillModel
                    {
                        SkillDescription = s.SkillDescription,
                        SkillParentId = s.SkillParentId,
                        SkillName = s.SkillName,
                        SkillId = s.SkillId,
                    }).ToList();
        }
    }
}
